package com.xiaowei.gym.gymjsp.ware.servlet;

import com.google.gson.Gson;
import com.xiaowei.gym.gymjsp.ware.dao.GymMenuDao;
import com.xiaowei.gym.gymjsp.ware.dao.UserDao;
import com.xiaowei.gym.gymjsp.ware.dao.impl.GymMenuDaoImpl;
import com.xiaowei.gym.gymjsp.ware.dao.impl.USerDaoImpl;
import com.xiaowei.gym.gymjsp.ware.entity.GymMenuEntity;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@WebServlet("/gym/DashboardServlet")
public class DashboardServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private GymMenuDao gymMenuDao;
    @Override
    public void init() throws ServletException {
        gymMenuDao=new GymMenuDaoImpl();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("request"+request);
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("loggedIn") == null) {
            response.sendRedirect("login.jsp");
            return;
        }
        String username = (String) session.getAttribute("user");
        RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/gymjsp/dashboard.jsp");
        dispatcher.forward(request, response); // 登录成功后重定向到欢迎页面
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }

//    private void buildMenuTreeRecursive(GymMenuEntity menu, Map<Integer, GymMenuEntity> menuMap) {
//        List<GymMenuEntity> children = new ArrayList<>();
//        for (GymMenuEntity child : menu.getChildren()) {
//            GymMenuEntity childMenu = menuMap.get(child.getId());
//            if (childMenu != null) {
//                buildMenuTreeRecursive(childMenu, menuMap);
//                children.add(childMenu);
//            }
//        }
//        menu.setChildren(children);
//    }
}